﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using Pantry.Business;
using Pantry.Commons;

namespace Pantry.Web.Users.Controls
{
    public partial class ListDepartment : System.Web.UI.UserControl
    {
        private DepartmentBus _departmentBus;
        private static DataView _dataView;

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            Page.Title = "View list department";
            RemoveActiveOnButton();
            LinkGroupAll.CssClass = "button-active";
            if (Request.QueryString["id"] != null)
            {
                FillterDepartment(CheckBoxIncludeActivate.Checked);
            }
        }
        /// <summary>
        /// Fillters the name of the gov office region by.
        /// </summary>
        /// <param name="includeInActived">if set to <c>true</c> [include in actived].</param>
        public void FillterDepartment(bool includeInActived)
        {
            try
            {
                _departmentBus = new DepartmentBus();
                int directorateId = int.Parse(Request.QueryString["id"]);
                DataTable listAllDepartment = _departmentBus.SelectDepartmentByDirectorateId(directorateId);
                _dataView = listAllDepartment.DefaultView;
                if (!includeInActived)
                {
                    _dataView.RowFilter = "IsActivated = 'Yes'";
                }

                CollectionPager.PageSize = ConstantSetup.DepartmentPageSize;
                CollectionPager.BindToControl = RepeaterListDepartment;
                CollectionPager.DataSource = _dataView;
                RepeaterListDepartment.DataSource = CollectionPager.DataSourcePaged;
                RepeaterListDepartment.DataBind();
            }
            catch (Exception ex)
            {
                ExceptionHandler.WriteErrorLog(ex);
                Response.Write(ex.Message);
            }

        }

        /// <summary>
        /// Fillters the name of the gov office region by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="includeInActived">if set to <c>true</c> [include in actived].</param>
        private void FillterDepartment(string name, bool includeInActived)
        {
            try
            {
                string condition = "";
                if (name.Length > -1)
                {
                    for (int i = 0; i < name.Length; i++)
                    {
                        if (i == 0)
                        {
                            condition += "(DepartmentName LIKE '" + name.Substring(i, 1) + "*' OR ";
                        }
                        else if (i == name.Length - 1)
                        {
                            if (!includeInActived)
                            {
                                condition += "DepartmentName LIKE '" + name.Substring(i, 1) + "*') AND IsActivated ='Yes'";
                            }
                            else
                            {
                                condition += "DepartmentName LIKE '" + name.Substring(i, 1) + "*')";
                            }
                        }

                        else
                        {
                            condition += "DepartmentName LIKE '" + name.Substring(i, 1) + "*' OR ";
                        }
                    }
                    _dataView.RowFilter = condition;
                }
                CollectionPager.PageSize = ConstantSetup.DepartmentPageSize;
                CollectionPager.BindToControl = RepeaterListDepartment;
                CollectionPager.DataSource = _dataView;
                RepeaterListDepartment.DataSource = CollectionPager.DataSourcePaged;
                RepeaterListDepartment.DataBind();
            }
            catch (ApplicationException ex)
            {
                ExceptionHandler.WriteErrorLog(ex);
            }
        }

        /// <summary>
        /// Checks the box include activate changed.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void CheckBoxIncludeActivateChanged(object sender, EventArgs e)
        {
            FillterDepartment(CheckBoxIncludeActivate.Checked);
        }

        /// <summary>
        /// Links the group all click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroupAllClick(object sender, EventArgs e)
        {
            FillterDepartment(CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroupAll.CssClass = "button-active";

        }
        /// <summary>
        /// Links the group0 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup0Click(object sender, EventArgs e)
        {
            FillterDepartment("0123456789", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup0.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group1 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup1Click(object sender, EventArgs e)
        {
            FillterDepartment("CBADE", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup1.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group2 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup2Click(object sender, EventArgs e)
        {
            FillterDepartment("FGHIJ", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup2.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group3 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup3Click(object sender, EventArgs e)
        {
            FillterDepartment("KLNM", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup3.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group4 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup4Click(object sender, EventArgs e)
        {
            FillterDepartment("OPQR", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup4.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group5 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup5Click(object sender, EventArgs e)
        {
            FillterDepartment("STUV", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup5.CssClass = "button-active";
        }
        /// <summary>
        /// Links the group6 click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void LinkGroup6Click(object sender, EventArgs e)
        {
            FillterDepartment("WXYZ", CheckBoxIncludeActivate.Checked);
            RemoveActiveOnButton();
            LinkGroup6.CssClass = "button-active";
        }

        /// <summary>
        /// Buttons the create click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonCreateClick(object sender, EventArgs e)
        {
            Response.Redirect("DepartmentDetails.aspx?action=create&directorateid=" + Request.QueryString["id"]);
        }

        /// <summary>
        /// Marks the activated.
        /// </summary>
        /// <param name="source">The source.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.RepeaterCommandEventArgs"/> instance containing the event data.</param>
        protected void MarkActivated(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
        {
            var hId = (HiddenField)RepeaterListDepartment.Items[e.Item.ItemIndex].FindControl("HiddenFieldDepartmentId");
            string[] values = hId.Value.Split('$');
            if (e.CommandName == "update")
            {
                int id = int.Parse(values[0]);
                _departmentBus = new DepartmentBus();
                bool result = _departmentBus.MarkActivated(id, true);
                //FillterDepartment(CheckBoxIncludeActivate.Checked);
                Response.Redirect("DepartmentDetails.aspx?action=update&id=" + values[0] + "&directorateid=" + values[1]);
            }
            else
            {
                Response.Redirect("DepartmentDetails.aspx?action=update&id=" + values[0] + "&directorateid=" + values[1]);
            }
        }
        protected void RemoveActiveOnButton()
        {
            LinkGroupAll.CssClass = "button-in-active";
            LinkGroup6.CssClass = "button-in-active";
            LinkGroup5.CssClass = "button-in-active";
            LinkGroup4.CssClass = "button-in-active";
            LinkGroup3.CssClass = "button-in-active";
            LinkGroup2.CssClass = "button-in-active";
            LinkGroup1.CssClass = "button-in-active";
            LinkGroup0.CssClass = "button-in-active";
        }
    }
}